/* SVN FILE: $Id: air_cache.js 37 2007-08-26 22:57:19Z killergeorge $ */
/**
 * To quote Dave Cardwell: 
 * Built on the shoulders of giants:
 *   * John Resig      - http://jquery.com/
 *
 * JamalJamal AdobeAir Settings component for JAMAL Javascript MVC Assembly Layout <http://jamal-mvc.com/>
 * Copyright (c)    2007, Georg Leciejewski
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright        Copyright (c) 2007, Georg Leciejewski
 * @link            
 * @package          jamal
 * @subpackage       jamal.airCache
 * @since            Jamal v 0.4
 * @version          $Revision: 37 $
 * @modifiedby       $LastChangedBy: killergeorge $
 * @lastmodified     $Date: 2007-08-26 15:05:38 +0200 (So, 26 Aug 2007) $
 * @license          http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Jamal AdobeAir Settings component
 *
 * @public
 * @name jamal
 * @cat message
 */
jamal.fn.extend({
    /* Constructor */
	/**
	 * Start the jamal settings file handling
	 *
     * @example 
     * @desc 
	 * @public
	 * @name airCache
	 * @type Object
	 * @cat airCache
	 */
     airCache: function () {

		if(!jamal.airCache.stream)
		{//init stream handler		
			jamal.airCache.stream 	= new air.FileStream();	
		}
		
		if(!jamal.airCache.cacheFile)
		{//init cache file handler	 	
        	jamal.airCache.cacheFile = air.File.applicationResourceDirectory;
			jamal.airCache.cacheFile = jamal.airCache.cacheFile.resolve('data/cache/' + jamal.airCache.filename);			
		}
		
     }
}); 

jamal.fn.extend(jamal.fn.airCache, {
	
	/**
	 *  the file handler
	 */
	cacheFile: null,
	
	/**
	 * the cache filename
	 */
	filename: 'cache.js',
	
	/**
	 * the file stream handler
	 */
	stream: null,	
	   
	/**
     * Returns the whole cache object
	 *
	 * @example jamal.airCache.read();
	 * @public
	 * @name jamal.airCache.read
	 * @type Function
	 * @cat airCache
	 * 
	 * @return string data - the file content
	 */
    read: function() {
		jamal.log('Start Component AirCache read');
		return this.readJS();			
		     
    },
    
	/**
	 * set a key val pair into the cache. overwriting existing or append new ones
	 * @example jamal.airCache.set('Whow','Dude')
	 * @public
	 * @name jamal.airCache.set
	 * @type Function
	 * @cat airCache 
	 * @param {String} name  - the name of the cache setting
	 * @param {String} value - the value for cache setting
	 */
    set: function(name, value) {
		jamal.log('Start Component AirCache set');
		var data  = this.readJS();
		var newdata = {};				
		//eval('newdata = {"'+ name + '" : "' + value + '"};');
		newdata[name]=value;
		//join those sweet little objects
		jQuery.extend(data, newdata);
		jamal.log('writing new cache values:');	
		jamal.log(data); 		
		this.saveJS(data);
	
    },	
	
	/**
	 * get a single cache value.
	 * this will throw an js exeption if the value is not present in the cache
	 * @example jamal.airCache.get('Whow') -> Dude
	 * @public
	 * @name jamal.airCache.get
	 * @type Function
	 * @cat airCache 
	 * @param String - the cache settings name
	 * @return Mixed - the value of the variable if found , else false
	 */
	get: function (name) {
		jamal.log('Start Component AirCache get');
		//read settings
		var data  = this.readJS();

		if(data[name])
		{//got some data			
			return data[name];
		}
		else
		{
			return false;	
		}
	},
	
	/**
	 * delete a cache value and saves the cache file
	 * @example jamal.airCache.del('Whow') -> Whow:Dude gets killed
	 * @public
	 * @name jamal.airCache.del
	 * @type Function
	 * @cat airCache 
	 * @param String - the cache settings name
	 */
	del: function (name) {
		jamal.log('Start Component AirCache del');
		var data  = this.readJS();
		if( data[name] )
		{ 
			delete data[name];
			this.saveJS(data);
		}	
	},
	
	/**
	 * kill the whole freakin cache file
	 * @public
	 * @name jamal.airCache.flush
	 * @type Function
	 * @cat airCache 
	 */
	flush: function () {
		jamal.log('Start Component AirCache flush');

		if (this.cacheFile.exists) 
		{//read data
			this.cacheFile.deleteFile();				
		}
	},
	
	/**
	* read the settings from the json file
	* @private
	* @name jamal.airCache.readJS
	* @type Function
	* @cat airCache 
	*/
	readJS: function () {
		jamal.log('Start Component AirCache readJS');
		
		var data 		= null;
		var json_text  	= '';

		if (this.cacheFile.exists) 
		{//read data
			this.stream.open(this.cacheFile , air.FileMode.READ);						
			json_text = this.stream.readUTFBytes(this.stream.bytesAvailable);
			this.stream.close();		
			data = eval('(' + json_text + ')');		
			return data;
		}
		else
		{//file does not exist yet, return an empty anonym object		
			return {};
		}			
	},	
	
	/**
	 * save new settings into an xml file.
	 * later do: if settings exist they are existing joined new vals are appended
	 * @private
	 * @name jamal.airCache.saveJS
	 * @type Function
	 * @cat airCache   
	 * @param {Object} data js object
	 */
	saveJS: function (data) {
		jamal.log('Start Component AirCache saveJS');
		var json = $.toJSON(data);
		//WRITE
		this.stream.open( this.cacheFile, air.FileMode.WRITE );			
		//save data
		this.stream.writeUTFBytes(json);
		//close stream
		this.stream.close();		
	}

});

